<template>
  <div class="wrap" @keyup.enter="doLogin">
    <div class="background"></div>
    <div class="login-wrap">
      <div class="title">用户登录</div>
      <div class="username">
        <span class="desc">用户名</span>
        <el-input
          class="username-input"
          placeholder="请输入用户名"
          v-model="username"
        >
          <i slot="prefix" class="el-input__icon el-icon-user-solid"></i>
        </el-input>
      </div>
      <div class="password">
        <span class="desc">密码</span>
        <el-input
          class="password-input"
          placeholder="请输入密码"
          v-model="password"
          show-password
        >
          <i slot="prefix" class="el-input__icon el-icon-lock"></i>
        </el-input>
      </div>
      <div class="login-btn-row">
        <el-button @click="doLogin" class="login-btn" type="primary"
          >登录</el-button
        >
      </div>
      <div class="options">
        <div class="return-index" @click="goToHome">返回首页</div>
      </div>
    </div>
  </div>
</template>
<script>
import { Input, Button } from "element-ui";
import { login } from "@/api/user.js";
export default {
  name: "login",
  components: {
    [Input.name]: Input,
    [Button.name]: Button,
  },
  data() {
    return {
      username: null,
      password: null,
    };
  },
  methods: {
    goToHome() {
      this.$router.push({ path: "/" });
    },
    doLogin() {
      login({
        username: this.username,
        password: this.password,
      })
        .then((res) => {
          this.$router.push({ path: "/" });
        })
        .catch((errMsg) => this.$alert(errMsg.msg));
    },
  },
};
</script>
<style lang="stylus" scoped>
.wrap {
  .background {
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    background-color: #92b7de;
    z-index: -1;
  }

  .login-wrap {
    width: 500px;
    margin: 0 auto;
    margin-top: 140px;
    border-radius: 8px;
    border: 1px solid #555;
    background-color: white;
    padding-bottom: 15px;

    .title {
      font-size: 20px;
      text-align: center;
      padding-top: 20px;
      padding-bottom: 20px;
      border-bottom: 1px solid #555;
      font-weight: bold;
      margin-bottom: 15px;
    }

    .username {
      .desc {
        position: absolute;
        line-height: 40px;
        padding-left: 15px;
      }

      .username-input {
        margin-left: 80px;
        width: 400px;
      }

      margin-bottom: 10px;
    }

    .password {
      .desc {
        position: absolute;
        line-height: 40px;
        padding-left: 15px;
      }

      .password-input {
        width: 400px;
        margin-left: 80px;
      }
    }

    .login-btn-row {
      padding-top: 5px;

      .login-btn {
        width: 465px;
        display: block;
        margin-left: 15px;
      }
    }

    .options {
      padding-left: 15px;
      margin-top: 15px;
      font-size: 14px;
      color: #aaa;
      user-select: none;

      .return-index {
        cursor: pointer;

        &:hover {
          color: black;
        }
      }
    }
  }
}
</style>